package com.lmiot.xyclick.Bean.SQL;

import android.content.Context;
import android.text.TextUtils;
import com.lmiot.xyclick.Bean.SQL.AutoBeanDao;
import com.lmiot.xyclick.Bean.SQL.DaoMaster;
import com.lmiot.xyclick.Util.FileUtils;
import com.lmiot.xyclick.inteface.OnBasicListener;
import com.zhy.android.percent.support.PercentLayoutHelper;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

public class AutoBeanSqlUtil {
    private static final AutoBeanSqlUtil ourInstance = new AutoBeanSqlUtil();
    private AutoBeanDao mAutoBeanDao;

    public static AutoBeanSqlUtil getInstance() {
        return ourInstance;
    }

    private AutoBeanSqlUtil() {
    }

    public void initDbHelp(Context context) {
        this.mAutoBeanDao = new DaoMaster(new DaoMaster.DevOpenHelper(context, "AutoBean-db", null).getWritableDatabase()).newSession().getAutoBeanDao();
    }

    public void add(AutoBean autoBean) {
        this.mAutoBeanDao.insertOrReplace(autoBean);
        FileUtils.saveToLocalData(autoBean.getAutoID(), new OnBasicListener() {
            @Override
            public void result(boolean z, String str) {
            }
        });
    }

    public void addNoLocal(AutoBean autoBean) {
        this.mAutoBeanDao.insertOrReplace(autoBean);
    }

    public void addList(List<AutoBean> list) {
        if (list != null && list.size() > 0) {
            this.mAutoBeanDao.insertOrReplaceInTx(list);
        }
    }

    public void delByID(String str) {
        FileUtils.delLocalData(str);
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.AutoID.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mAutoBeanDao.delete((AutoBean) arrayList.get(0));
        }
    }

    public void delByName(String str) {
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.AutoName.eq(str), new WhereCondition[0]).orderDesc(AutoBeanDao.Properties.SortNum).list();
        if (arrayList.size() > 0) {
            this.mAutoBeanDao.delete((AutoBean) arrayList.get(0));
        }
    }

    public void delAll() {
        try {
            this.mAutoBeanDao.deleteInTx(this.mAutoBeanDao.queryBuilder().build().list());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public AutoBean searchByName(String str) {
        QueryBuilder queryBuilder = this.mAutoBeanDao.queryBuilder();
        Property property = AutoBeanDao.Properties.AutoName;
        ArrayList arrayList = (ArrayList) queryBuilder.where(property.like("%" + str + "%"), new WhereCondition[0]).orderDesc(AutoBeanDao.Properties.SortNum).list();
        if (arrayList.size() > 0) {
            return (AutoBean) arrayList.get(0);
        }
        return null;
    }

    public AutoBean searchByNameRrigh(String str) {
        ArrayList arrayList = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.AutoName.eq(str), new WhereCondition[0]).orderDesc(AutoBeanDao.Properties.SortNum).list();
        if (arrayList.size() > 0) {
            return (AutoBean) arrayList.get(0);
        }
        return null;
    }

    public AutoBean searchByID(String str) {
        if (str == null) {
            return null;
        }
        ArrayList arrayList = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.AutoID.eq(str), new WhereCondition[0]).orderDesc(AutoBeanDao.Properties.SortNum).list();
        if (arrayList.size() > 0) {
            return (AutoBean) arrayList.get(0);
        }
        return null;
    }

    public List<AutoBean> searchAllAction(String str) {
        ArrayList arrayList = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.AutoType.eq(str), new WhereCondition[0]).orderDesc(AutoBeanDao.Properties.SortNum).list();
        return arrayList == null ? new ArrayList() : arrayList;
    }

    public List<AutoBean> searchAllByShowNotic() {
        ArrayList arrayList = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.ShowNotic.eq(true), new WhereCondition[0]).list();
        return arrayList == null ? new ArrayList() : arrayList;
    }

    public List<AutoBean> searchAllByType(String str) {
        ArrayList arrayList = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.AutoType.eq(str), new WhereCondition[0]).list();
        return arrayList == null ? new ArrayList() : arrayList;
    }

    public List<AutoBean> searchAllNotic() {
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.AutoType.eq("notic"), new WhereCondition[0]).orderDesc(AutoBeanDao.Properties.SortNum).list();
        if (arrayList2 != null) {
            arrayList.addAll(arrayList2);
        }
        ArrayList arrayList3 = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.AutoType.eq("notic_now"), new WhereCondition[0]).list();
        if (arrayList3 != null) {
            arrayList.addAll(arrayList3);
        }
        return arrayList;
    }

    public List<AutoBean> searchAllTime(String str, boolean z) {
        try {
            ArrayList arrayList = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.AutoType.eq(str), AutoBeanDao.Properties.IsEnable.eq(Boolean.valueOf(z))).orderDesc(AutoBeanDao.Properties.SortNum).list();
            return arrayList == null ? new ArrayList() : arrayList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList();
        } catch (Throwable unused) {
            return new ArrayList();
        }
    }

    public List<AutoBean> searchAllByGroup(String str) {
        ArrayList arrayList = (ArrayList) this.mAutoBeanDao.queryBuilder().where(AutoBeanDao.Properties.GroupID.eq(str), new WhereCondition[0]).orderDesc(AutoBeanDao.Properties.SortNum).list();
        return arrayList == null ? new ArrayList() : arrayList;
    }

    public List<AutoBean> searchAllByGroupOther(String str) {
        List<AutoBean> searchAll = searchAll();
        searchAll.removeAll(searchAllByGroup(str));
        return searchAll;
    }

    public List<AutoBean> searchAll() {
        List<AutoBean> list = this.mAutoBeanDao.queryBuilder().orderDesc(AutoBeanDao.Properties.SortNum).build().list();
        return list == null ? new ArrayList() : list;
    }

    public void recoverGroupData() {
        List<AutoBean> searchAll = searchAll();
        if (searchAll.size() > 0) {
            for (AutoBean autoBean : searchAll) {
                String groupID = autoBean.getGroupID();
                if (TextUtils.isEmpty(groupID)) {
                    autoBean.setGroupID("666");
                } else if (GroupBeanSqlUtil.getInstance().searchByID(groupID) == null) {
                    autoBean.setGroupID("666");
                }
            }
            addList(searchAll);
        }
    }
}
